package com.jdz.gxq.info.admin.estate.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jdz.gxq.info.admin.estate.bo.Estate;
import com.jdz.gxq.info.admin.estate.service.EstateService;
import com.jdz.gxq.info.admin.file.service.FileService;
import com.jdz.gxq.info.common.action.CommonAction;
import com.jdz.gxq.info.common.constant.Constant;

public class DeleteEstateAction extends CommonAction {

	private EstateService estateService = null;
	
	private FileService fileService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String enterpriseIdStr = request.getParameter("id");
		
		String[] enterpriseIds = enterpriseIdStr.split(",");
		
		// 文件上传相对路径
		String relativePath = Constant.IMAGE_PATH_ESTATE;
		
		// 取得的物理路径
		String realPath = request.getSession().getServletContext().getRealPath(relativePath);
		
		for (String enterpriseId : enterpriseIds) {
			
			Estate estate = estateService.getEstateById(Integer.parseInt(enterpriseId));
			
			String enterprisePicture = estate.getEnterprisePicture();
			
			if (!StringUtils.isEmpty(enterprisePicture)) {
				
				String[] picIds = enterprisePicture.split(",");
				
				for (String picId : picIds) {
					fileService.deleteImageInfo(picId, realPath);
				}
			}
			
			estateService.deleteEstate(Integer.parseInt(enterpriseId));
		}

		return mapping.findForward("success");
	}

	public void setEstateService(EstateService estateService) {
		this.estateService = estateService;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
}

